home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int attributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- int width = 200, height = 200;
-
- static void DoDisplay(GLfloat tx, GLfloat ty, GLfloat rx, GLfloat rz)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.5, width - 0.5, -0.5, height - 0.5, 0.01, 1000.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(100 + tx, 100 + ty, -10);
- glRotatef(rx, 1, 0, 0);
- glRotatef(rz, 0, 0, 1);
- glScalef(20, 20, 20);
-
- glViewport(0, 0, width, height);
- glClearColor(0.25, 0.25, 0.25, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glShadeModel(GL_FLAT);
-
- /* Draw a bunch of triangles that converge on a single point */
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(1, 0, 0);
- glVertex2f(0, 0);
- glVertex2f(-1, -0.5);
- glVertex2f(-1, -0.25);
- glColor3f(0, 1, 0);
- glVertex2f(-1, 0);
- glColor3f(0, 0, 1);
- glVertex2f(-1, 0.25);
- glColor3f(1, 1, 1);
- glVertex2f(-1, 0.5);
- glEnd();
-
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(1, 0, 0);
- glVertex2f(0, 0);
- glVertex2f(-0.5, 1);
- glVertex2f(-0.25, 1);
- glColor3f(0, 1, 0);
- glVertex2f(0, 1);
- glColor3f(0, 0, 1);
- glVertex2f(0.25, 1);
- glColor3f(1, 1, 1);
- glVertex2f(0.5, 1);
- glEnd();
-
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(1, 0, 0);
- glVertex2f(0, 0);
- glVertex2f(1, 0.5);
- glVertex2f(1, 0.25);
- glColor3f(0, 1, 0);
- glVertex2f(1, 0);
- glColor3f(0, 0, 1);
- glVertex2f(1, -0.25);
- glColor3f(1, 1, 1);
- glVertex2f(1, -0.5);
- glEnd();
-
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(1, 0, 0);
- glVertex2f(0, 0);
- glVertex2f(-0.5, -1);
- glVertex2f(-0.25, -1);
- glColor3f(0, 1, 0);
- glVertex2f(0, -1);
- glColor3f(0, 0, 1);
- glVertex2f(0.25, -1);
- glColor3f(1, 1, 1);
- glVertex2f(0.5, -1);
- glEnd();
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(int argc, char *argv[])
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- GLfloat tx = 0;
- GLfloat ty = 0;
- GLfloat rx = 0;
- GLfloat rz = 0;
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocNone);
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- width, height,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetStandardProperties(dpy, window, "tri", "tri", None, argv, argc, NULL);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- width = event.xconfigure.width;
- height = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_p:
- case XK_P:
- glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
- needDisplay = GL_TRUE;
- break;
- case XK_l:
- case XK_L:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- needDisplay = GL_TRUE;
- break;
- case XK_f:
- case XK_F:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- needDisplay = GL_TRUE;
- break;
- case XK_Left:
- tx -= 0.1;
- needDisplay = GL_TRUE;
- break;
- case XK_Right:
- tx += 0.1;
- needDisplay = GL_TRUE;
- break;
- case XK_Up:
- ty += 0.1;
- needDisplay = GL_TRUE;
- break;
- case XK_Down:
- ty -= 0.1;
- needDisplay = GL_TRUE;
- break;
- case XK_KP_Left:
- rz += 1;
- needDisplay = GL_TRUE;
- break;
- case XK_KP_Right:
- rz -= 1;
- needDisplay = GL_TRUE;
- break;
- case XK_KP_Up:
- rx += 1;
- needDisplay = GL_TRUE;
- break;
- case XK_KP_Down:
- rx -= 1;
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay(tx, ty, rx, rz);
- glFlush();
- }
- }
- }
-